﻿using Cxuu.Common;
using Cxuu.Models;
using Microsoft.Extensions.DependencyInjection;
using PetaPoco;
using PetaPoco.Core;
using System.Threading.Tasks;

namespace Cxuu.Repository
{
    [UseDI(ServiceLifetime.Scoped)]
    public class ConfigRepository : BaseRepository<Config, PetaPocoContext>
    {

        public ConfigRepository(PetaPocoContext petaPocoContext) : base(petaPocoContext) { }

        public async Task<Config> GetByNameAsync(string name)
        {
            PocoData data = PocoData.ForType(typeof(Config), new ConventionMapper());
            Sql sql = Sql.Builder.Select("*");
            sql.From(new object[] { data.TableInfo.TableName });
            sql.Where("Name = @0", name);
            return await CreateDAO().SingleOrDefaultAsync<Config>(sql);
        }

        public virtual async Task<int> UpdateByNameAsync(Config config, string primaryKeyValue)
        {
            return await CreateDAO().UpdateAsync(config, primaryKeyValue);
        }
    }
}
